<?php
ArrowController::importModel("org.arrowplatform.controls.forms.IArrowFormField");
abstract class ArrowFormField extends ArrowTag implements IArrowFormField{
	
	private $name;
	private $redirectToView = false;
	private $namespace = true;
	private $value;
	private $currentValue; 
	private $check = true;
	private $default = null;
	private $hint = false;
	public $alert = null;
	public $access = false;
	
	/**
	 * Field form
	 *
	 * @var ArrowForm
	 */
	protected $form;
	
	
	public function configure(){
		
		$this->setErrorOnUnknowAttribute( false );
	
		$this->addProperties(array(
				"name" => null,
				"value" => false,
				"content" => false,
				"check" => true,
				"alert" => false,
				"namespace" => true,
        		"checkifempty" => true,
				"default" => null,
				"hint" => false
		));
		
		$this->addRequiredProperties(array("name"));
	}
	
	public function init(){
		$this->form = $this->getParent("ArrowForm");
		
		$properties = $this->getProperties();
		$name = $this->getProperty("name");
		if( $this->form != null ) $this->access = $this->form->checkAccess( $name );
		else $this->access = true ;
		
		if((substr($properties["name"],0,3)=="_d_")){
			$this->redirectToView = true;
			$this->name = substr($name,3);
		}else{
			$this->name = $name;
		}
		$this->value = isset($properties["value"])?$properties["value"]:false;
		$this->alert = isset($properties["alert"])?$properties["alert"]:false;
		
		$this->default = isset($properties["default"])?$properties["default"]:null;
		$this->hint = isset($properties["hint"])?$properties["hint"]:false;
		
		if(isset($properties["check"]))
			if($properties["check"] == "0")
				$this->check = false;
				
		if(isset($properties["namespace"]))
			if($properties["namespace"] == "0")
				$this->namespace = false;
			else
				$this->namespace = $properties["namespace"]; 
		
	}
	
	
	public function getName(){
		return $this->getProperty("name");
	}
	
	public function getHint(){
		return $this->hint;
	}
	
	
	public function getNamespace(){
		if( $this->namespace && $this->namespace != 1)
			return $this->namespace;
		if( $this->form && $this->namespace && $formNamespace = $this->form->getNamespace() )
			return $formNamespace;
		return false;
	}
	
	public function getHtmlName(){
		$namespace  = $this->getNamespace();
		if($namespace){
			if($this->redirectToView)	
				return $namespace."[_d_".$this->name."]";
			return $namespace."[".$this->name."]";
		}
		if($this->redirectToView)	
			return "_d_".$this->name;
		return $this->name;
	}
	
	public function getContent(){
		$localContent = $this->getProperty("content");
		if( $localContent != false )
			return $localContent;
		
		$dataSource = $this->getFirstChild("ArrowCtrlDatasource");
		if($dataSource != null)
			return $dataSource->getData();
			
		if($this->form)
			return $this->form->getContent( $this->name );
		return false;
	}
	
	public function isContentCodeKept(){ return false; }
	public function generateBlockStart(){}
	public function generateBlockEnd(){  }
	public function generateBlockStartCode(){}
	public function generateBlockEndCode(){ return $this->generateOutputCode(); }
	
	
	public function getValue(){
		$currentValue = $this->getCurrentValue();
		if($currentValue !== false)
			return $currentValue;
		
		if($this->value === "")
			return 	"";
		
		if($this->value === false && $this->form){
			$setter = $this->form->getValuesSetter();
			if( !is_null($setter)  )
				return $setter->formGetFieldValue( $this->name, $this->form->getId() );
		}
		$value = $this->getProperty("value");
		if($value !== false)
			return $value;
		
		return $this->getProperty("default");
	}
	
	public function setValue( $value ){
		$this->value = $value;
	}
	
	protected function getCurrentValue(){
		if($this->form){
			$values = $this->form->getProposedValues();
			if( isset( $values[$this->getName()] ) )
				return $values[$this->getName()];
		}
		return false;
	}
	

	
	public function isCheckable(){
		return $this->check;		
	}
	
	public function getAlert(){
		return $this->alert;
	}
	

	public static function genTagsFromArray( $array ){
		$str="";
		foreach($array as $paramName=>$paramValue){
			$str.= $paramName.'="'.$paramValue.'" ';
		}
		return $str;		
	}
	
	protected function addExternal(){
		$str="";
		$classExists = false;
		foreach($this->getExternalProperties() as $paramName=>$paramValue){
			if($paramName == "class"){
				if( $this->getProperty("check") == 1 )
					$paramValue.= " ctrl-field-required";
				$classExists = true;
			}
			$str.= $paramName.'="'.$paramValue.'" ';
		}
		if(!$classExists &&  $this->getProperty("check") == 1)
			$str.= 'class="ctrl-form-field-required" ';
		return $str;
	}
	
	public function generate(){
		if( $this->access ) {
			$ret = $this->generateOutput() ;
			//$ret .= print_r( $this->getProperties(), 1 ) ;
			//if( $this->getProperty("check") == 1 ) $ret .= "<a class=\"field_required\" href=\"javascript:void(0);\" title=\"pole wymagane\"></a>" ;
			return $ret ;
		} 
	
		return  "---".$this->getName() ;
	}
	
	
}

?>